package com.timesheet.gui.efforttracker.validators;

import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.fieldassist.ControlDecoration;

public class NotNullValidator implements IValidator {

	private String message;
	private ControlDecoration controlDecoration = null;

	public NotNullValidator(String message, ControlDecoration controlDecoration) {
		this.message = message;
		this.controlDecoration = controlDecoration;

	}
	public NotNullValidator(String message) {
		this.message = message;
		
	}

	@Override
	public IStatus validate(Object value) {
		if (value instanceof String) {
			String string = (String) value;
			if (string != null && string.length() > 0) {
				controlDecoration.hide();
				return Status.OK_STATUS;
			} else {
				controlDecoration.show();
				return ValidationStatus.error(message);
			}
		}
		return Status.OK_STATUS;

	}

}
